package org.csu.jpetmanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import jakarta.servlet.http.HttpSession;
import org.csu.jpetmanage.mapper.ItemMapper;
import org.csu.jpetmanage.mapper.ProductMapper;
import org.csu.jpetmanage.model.Category;
import org.csu.jpetmanage.model.Item;
import org.csu.jpetmanage.model.Product;
import org.csu.jpetmanage.service.ItemService;
import org.csu.jpetmanage.service.ProductService;
import org.csu.jpetmanage.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
//@RequestMapping(value = "/Item")
public class ItemController {
    @Autowired
    ItemService itemService;

    @Autowired
    Item item;

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ProductService productService;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    Product product;

    String message = null;

    @GetMapping("/displayItem")
    public String displayItem( Model model){
        List<Item> list =itemService.list();
        model.addAttribute("itemList" ,list);
        List<Product> productlist =productService.list();
        model.addAttribute("productList" ,productlist);

        return "yuan/item";
    }



    @RequestMapping("/addItem")
    public String addItem(
            @RequestParam("itemid") String itemid,
            @RequestParam("productid") String productid,
            @RequestParam("listprice") String listprice,
            @RequestParam("attr1") String attr1,
            @RequestParam("attr2") String attr2,
            @RequestParam("attr3") String attr3,
            @RequestParam("attr4") String attr4,
            MultipartFile file,
            Model model,
            HttpSession session) throws IOException {
        List<Item> list =itemService.list();
        model.addAttribute("itemList" ,list);
        item.setItemid(itemid);
        item.setProductid(productid);
        item.setListprice(listprice);
        item.setAttr1(attr1);
        item.setAttr2(attr2);
        item.setAttr3(attr3);
        item.setAttr4(attr4);
        item.setAttr5(UploadUtil.uploadImage(file));
        itemMapper.insert(item);
        session.setAttribute("msg","插入成功");
        return "redirect:/displayItem";
    }

    @RequestMapping("/deleteItem")
    public String deleteItem(
            @RequestParam("itemid") String itemid,
            Model model,
            HttpSession session
    ){
        List<Item> list =itemService.list();
        model.addAttribute("itemList" ,list);
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("itemid",itemid);
        int sum = itemMapper.deleteByMap(hashMap);
        if (sum>=1){
            session.setAttribute("msg","删除成功");
        }
        return "redirect:/displayItem";
    }

    @GetMapping("/itemEditAjax")
    @ResponseBody
    public Map<String,Object> orderEditAjax(
            @RequestParam("itemid") String itemid,
            HttpSession session
    ){
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemid",itemid);
        List<Item> itemList = itemMapper.selectList(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        if (itemList.size()==1){
            item = itemList.get(0);
            session.setAttribute("updateItem",item);//待会处理编辑数据的时候需要用
            //把下面补全
            map.put("itemid",item.getItemid());
            map.put("productid",item.getProductid());
            map.put("listprice",item.getListprice());
            map.put("attr1",item.getAttr1());
            map.put("attr2",item.getAttr2());
            map.put("attr3",item.getAttr3());
            map.put("attr4",item.getAttr4());
//            map.put("attr5",item.getAttr5());
            return map;
        }
        return map;
    }

    @RequestMapping("/updateItem")
    public String updateItem(
            @RequestParam("updateitemid") String itemid,
            @RequestParam("updateproductid") String productid,
            @RequestParam("updatelistprice") String listprice,
            @RequestParam("updateattr1") String attr1,
            @RequestParam("updateattr2") String attr2,
            @RequestParam("updateattr3") String attr3,
            @RequestParam("updateattr4") String attr4,
            @RequestParam("file") MultipartFile file,
//            @RequestParam("updateattr5") String attr5,

            Model model,
            HttpSession session) throws IOException {
        item = (Item)session.getAttribute("updateItem");
        item.setItemid(itemid);
        item.setProductid(productid);
        item.setListprice(listprice);
        item.setAttr1(attr1);
        item.setAttr2(attr2);
        item.setAttr3(attr3);
        item.setAttr4(attr4);
        item.setAttr5(UploadUtil.uploadImage(file));
        UpdateWrapper<Item> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("itemid",item.getItemid());
        int rows = itemMapper.update(item,updateWrapper);
        session.setAttribute("msg","修改成功");
        return "redirect:/displayItem";
    }

    @RequestMapping("/searchItem")
    public String searchItem(
            @RequestParam("keyword") String keyword,
            Model model){
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("itemid",keyword).
                or().like("productid",keyword).
                or().like("listprice",keyword).
                or().like("attr1",keyword).
                or().like("attr2",keyword).
                or().like("attr3",keyword).
                or().like("attr4",keyword).
                or().like("attr5",keyword);
        List<Item> itemList=itemMapper.selectList(queryWrapper);
        model.addAttribute("itemList" ,itemList);
        List<Product> productlist =productService.list();
        model.addAttribute("productList" ,productlist);
        return "yuan/item";
    }

    @GetMapping("/itemSearchAjax")
    @ResponseBody
    public List<Item> itemSearchAjax(
            @RequestParam("keyword") String keyword,
            HttpSession session){
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("itemid",keyword).
                or().like("productid",keyword).
                or().like("listprice",keyword).
                or().like("attr1",keyword).
                or().like("attr2",keyword).
                or().like("attr3",keyword).
                or().like("attr4",keyword).
                or().like("attr5",keyword);
        return itemMapper.selectList(queryWrapper);
    }
}
